package com.dcjt.firehotcarpad.utils

import com.aries.library.fast.manager.LoggerManager
import com.aries.library.fast.retrofit.FastMultiUrl
import com.dcjt.firehotcarpad.base.ApiPath

import sun.misc.BASE64Decoder
import sun.misc.BASE64Encoder
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream

object MessageUtils {
    var baseUrl = ""
    const val appName = "消防接处警node版"
    var systemMessage = "systemonlinegroup"

    val url =     FastMultiUrl.getInstance().getBaseUrl( ApiPath.KEY_BASE_MESSAGE)


    var pmqToken = ""
    var pmqAid = ""

    /**
     * 解压消息内容
     */
    fun uncompress(value: String): String {
        var result: String = ""
        var out: ByteArrayOutputStream? = null
        var byteArrayIS: ByteArrayInputStream? = null
        var gzipInputStream: GZIPInputStream? = null
        try {
            out = ByteArrayOutputStream()
            val compressed = BASE64Decoder().decodeBuffer(value)
            byteArrayIS = ByteArrayInputStream(compressed)
            gzipInputStream = GZIPInputStream(byteArrayIS)
            val buffer = ByteArray(1024)
            var offset = -1
            while (gzipInputStream.read(buffer).also { offset = it } != -1) {
                out.write(buffer, 0, offset)
            }
            result = out.toString()
        } finally {
            out?.close()
            byteArrayIS?.close()
            gzipInputStream?.close()
        }
        return result
    }

    fun compress(value: String): String {
        if (value.isEmpty()) return value
        val byteArrayOutputStream = ByteArrayOutputStream()
        val gzipOutputStream = GZIPOutputStream(byteArrayOutputStream)
        try {
            gzipOutputStream.write(value.toByteArray())
        } finally {
            byteArrayOutputStream.close()
            gzipOutputStream.close()
        }
        return BASE64Encoder().encode(byteArrayOutputStream.toByteArray())
    }

    /**
     * 自定义加密
     * @param {json} code 数据
     */
    fun compile(code: String): String {
        val info = StringBuilder()
        info.append((code[0].toInt() + code.length).toChar())
        for (i in 1 until code.length) {
            info.append((code[i].toInt() + code[i - 1].toInt()).toChar())
        }
        return info.toString()
    }

    /**
     * 自定义解密
     * @param {*} code 数据
     */
    fun unCompile(value: String): String {
//        val code = unescape(value)
        val code = Escape.unescape(value)
        val info = StringBuilder()
        info.append((code[0].toInt() - code.length).toChar())
        for (i in 1 until code.length) {
            info.append((code[i].toInt() - info[i - 1].toInt()).toChar())
        }
        LoggerManager.d("解压内容：${info.toString()}")
        return info.toString()
    }

    private fun escape(src: String): String {
        var i: Int = 0
        var j: Char
        val tmp = StringBuffer()
        tmp.ensureCapacity(src.length * 6)
        while (i < src.length) {
            j = src[i]
            if (Character.isDigit(j) || Character.isLowerCase(j)
                || Character.isUpperCase(j)
            ) tmp.append(j) else if (j.toInt() < 256) {
                tmp.append("%")
                if (j.toInt() < 16) tmp.append("0")
                tmp.append(j.toInt().toString(16))
            } else {
                tmp.append("%u")
                tmp.append(j.toInt().toString(16))
            }
            i++
        }
        return tmp.toString()
    }

    fun unescape(src: String): String {
        val tmp = StringBuffer()
        tmp.ensureCapacity(src.length)
        var lastPos = 0
        var pos = 0
        var ch: Char
        while (lastPos < src.length) {
            pos = src.indexOf("%", lastPos)
            if (pos == lastPos) {
                if (src[pos + 1] == 'u') {
                    ch = src
                        .substring(pos + 2, pos + 6).toInt(16).toChar()
                    tmp.append(ch)
                    lastPos = pos + 6
                } else {
                    ch = src
                        .substring(pos + 1, pos + 3).toInt(16).toChar()
                    tmp.append(ch)
                    lastPos = pos + 3
                }
            } else {
                lastPos = if (pos == -1) {
                    tmp.append(src.substring(lastPos))
                    src.length
                } else {
                    tmp.append(src.substring(lastPos, pos))
                    pos
                }
            }
        }
        return tmp.toString()
    }

}



